/*
 * @Description: 接口请求统一配置
 * @Autor: chenyt
 * @Date: 2020-03-21 21:18:41
 * @LastEditors: GiottoLLL7
 * @LastEditTime: 2023-08-01 10:47:53
 * @LastEditors: syx
 * @LastEditTime: 2021-11-25 16:05:44
 */
import axios from "axios"
import {getToken, setToken, removeToken} from "@/utils/auth"
import router from "@/router"
import {
  // Message,
  Loading,
  Dialog
} from "vant"
import sm from  "miniprogram-sm-crypto"
const sm2=sm.sm2
let enableSm2=true
const publicKey='049251aeb160240417aca332022db37648750515b11fc62539fae6bd7a9ba16e4a8d31730ae705e99c811eae691ff06cf0d2dc3802306d9c6d57ca3a6d42986d29'

// import Cookies from 'js-cookie'
// 环境变量
const API_BASEURL = import.meta.env.VITE_BASE_URL//api基础路径
// const CLIENT_TIMEOUT = 60000//链接时间
const CLIENT_TIMEOUT = import.meta.env.APP_TIMEOUT || 6000000000//链接时间
// const enableMock = process.env.VUE_APP_ENABLE_MOCK === "true"//是否开启https://www.apipark.cn/s/a762b67e-a9c4-4d16-8bcc-4bfce87dc7ad/http/5447951
const service = axios.create({
  baseURL: API_BASEURL,
  timeout: CLIENT_TIMEOUT
  //headers: {
  //  "Content-Type": "application/json"
  //}
})
service.defaults.timeout = CLIENT_TIMEOUT
//请求
service.interceptors.request.use(config => {
  if (config.content){
    axios.defaults.headers['Content-Type'] = config.content
  }else {
    axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
  }
  const token = getToken()
  if (token) {
    config.headers["Authorization"] = token
  }
  config.headers["FN-Channel"] = "device"
  if (config.method === 'post'){
    if(enableSm2&&axios.defaults.headers['Content-Type']&&axios.defaults.headers['Content-Type'].toLowerCase().startsWith("application/json")){
      if(config.data){
        config.headers['TransSecurity'] = '1'
        let text=JSON.stringify(config.data);
        let cipher=sm2.doEncrypt(text, publicKey)
        config.data={cipher}

      }
    }

  }
  return config
}, error => {
  return Promise.reject(error)
})

//响应拦截
service.interceptors.response.use(response => {
  const res = response.data
  const url = response.config.url
  if (response.status !== 200) {
    Dialog.alert({
      title: "系统提示",
      message: res.message || "Error",
      theme: 'round-button'
    })
    return Promise.reject(new Error(res.message || "Error"))
  } else {
    if (response.config.showError === false){
      return res
    }
    if (res.status == "200") {
      if ("/device/login" === url){
        setToken(response.headers.authorization)
      }
      return res
    } else if (res.status == "604") {
      if ("/device/logout" !== url) {
        Dialog.alert({
          title: "系统提示",
          message: "请先登录",
          theme: 'round-button'
        }).then(()=>{
          window.location.href = "/login"
        })
      }
      return Promise.reject(new Error(res.message || "Error"))
    } else {
      Dialog.alert({
        title: "系统提示",
        message: res.message || "Error",
        theme: 'round-button'
      })
      return Promise.reject(new Error(res.message || "Error"))
    }
  }
}, error => {
  Dialog.alert({
    title: "系统提示",
    message: "服务器开小差了，请重新加载",
    theme: 'round-button'
  })
  return Promise.reject(error)
})
export default service
